# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libpeas
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-docs")
pkgver=1.36.0
pkgrel=3
pkgdesc="A GObject-based plugins engine (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://wiki.gnome.org/Projects/Libpeas'
msys2_references=(
  "cpe: cpe:/a:gnome:libpeas"
)
license=('spdx:LGPL-2.1-or-later')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-glade"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-python-gobject"
             "${MINGW_PACKAGE_PREFIX}-gi-docgen"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-vala")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gtk3"
         "${MINGW_PACKAGE_PREFIX}-adwaita-icon-theme"
         "${MINGW_PACKAGE_PREFIX}-gtk-update-icon-cache"
         "${MINGW_PACKAGE_PREFIX}-gobject-introspection-runtime"
        )
source=(https://download.gnome.org/sources/${_realname}/${pkgver:0:4}/${_realname}-${pkgver}.tar.xz
        fix-build.patch)
sha256sums=('297cb9c2cccd8e8617623d1a3e8415b4530b8e5a893e3527bbfd1edd13237b4c'
            '49acc06aaf973c7d516db86ac77fe075e6fea26e38af8bea56bd609b54fd407d')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  patch -p1 -i "${srcdir}"/fix-build.patch
}

build() {
  local -a _static_flags=(
    -DGIO_STATIC_COMPILATION
    -DGLIB_STATIC_COMPILATION
    -DGMODULE_STATIC_COMPILATION
    -DGOBJECT_STATIC_COMPILATION
  )

  local -a _meson_options=(
    --prefix="${MINGW_PREFIX}"
    --wrap-mode=nodownload
    -Ddemos=true
    -Dwidgetry=true
    -Dpython2=false
    -Dvapi=true
  )

  CFLAGS+=" ${_static_flags[@]}" \
  CXXFLAGS+=" ${_static_flags[@]}" \
  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson.exe setup \
    "${_meson_options[@]}" \
    --default-library=static \
    -Dgtk_doc=false \
    -Dintrospection=false \
    "${_realname}-${pkgver}" \
    "build-${MSYSTEM}-static"

  ${MINGW_PREFIX}/bin/meson.exe compile -C "build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson.exe setup \
    "${_meson_options[@]}" \
    --default-library=shared \
    -Dgtk_doc=true \
    -Dintrospection=true \
    "${_realname}-${pkgver}" \
    "build-${MSYSTEM}-shared"

  ${MINGW_PREFIX}/bin/meson.exe compile -C "build-${MSYSTEM}-shared"
}

package_libpeas() {
  ${MINGW_PREFIX}/bin/meson.exe install -C "build-${MSYSTEM}-static" --destdir "${pkgdir}"
  ${MINGW_PREFIX}/bin/meson.exe install -C "build-${MSYSTEM}-shared" --destdir "${pkgdir}"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"

  # split docs
  mkdir -p dest${MINGW_PREFIX}/share
  mv "${pkgdir}${MINGW_PREFIX}"/share/doc dest${MINGW_PREFIX}/share/doc
}


package_libpeas-docs() {
  pkgdesc+=" (documentation)"
  depends=()

  mv dest/* "${pkgdir}"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
